<template>
<el-container class="home_container">
  <!-- 头部区域 -->
  <el-header>
    <div>
      
      <a href="http://www.le.com/ptv/vplay/29367033.html">
        <img src="~@/assets/qz.jpg" alt="">
      </a>
      <span>电商管理后台系统</span>
    </div>
    <el-button type="info" @click="loginout">
        退出
    </el-button>
  </el-header>
  <!-- 主要内容区 -->
  <el-container>
    <el-aside :width="isCol ? '64px' : '200px'">
      <div class="toggleCollapse" @click="isCollapse">| | |</div>
      <el-menu
        background-color="#323744"
        text-color="#fff"
        active-text-color="#0984e3"
        unique-opened
        :collapse= "isCol"
        :collapse-transition="false"
        router
        :default-active="activePath"
        >
        <!-- 一级导航 -->
        <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
          <!-- 一级导航内容 -->
          <template slot="title">
            <!-- 图标 -->
            <i :class="itemObj[item.id]" style="margin-right:10px"></i>
            <!-- 文本 -->
            <span>{{item.authName}}</span>
          </template>
          <!-- 二级导航菜单 -->
          <el-menu-item :index="'/' + subitem.path" 
                        v-for="subitem in item.children" 
                        :key="subitem.id"
                        @click="savePath('/' + subitem.path)"
                        >
            <i class="el-icon-menu"></i>
            <span>{{subitem.authName}}</span>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      itemObj: {
        '125':'iconfont icon-users',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao',
      },
      isCol: false,
      activePath: '',
    }
  },
  created () {
    this.getMenuInfo();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout() {
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    async getMenuInfo () {
     const {data: res} = await this.$http.get('menus') 
     if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
     this.menuList = res.data;
    },
    isCollapse () {
      this.isCol = !this.isCol
    },
    // 保存激活高亮的值
    savePath (activePath) {
      window.sessionStorage.setItem('activePath',activePath);
       this.activePath = activePath;
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
img {
  border-radius: 40px;
}
.el-header {
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #323744;
  .el-menu {
    border-right: none;
  }
}
.el-container {
  background-color: #ededf1;
}
.toggleCollapse {
  background-color: #4a5064;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  cursor: pointer;

}
</style>